package talayaco.gui;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;

import talayaco.enums.TipoAnimalEnum;
import talayaco.interfaz.GestionAnimalesINT;
import talayaco.interfaz.GestionCorralesINT;
import talayaco.utils.ErrorUtils;
import talayaco.utils.StringUtils;

public class AgregarAnimales extends javax.swing.JInternalFrame {

	private static final long serialVersionUID = -2293147131728229874L;

	public AgregarAnimales() {
		initComponents();
	}

	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		jLabel1 = new javax.swing.JLabel();
		listadoTipoAnimal1 = new javax.swing.JComboBox();
		jLabel2 = new javax.swing.JLabel();
		cantidad1 = new javax.swing.JTextField();
		jLabel3 = new javax.swing.JLabel();
		listadoCorrales1 = new javax.swing.JComboBox();
		jButton1 = new javax.swing.JButton();
		botonAgregar = new javax.swing.JButton();

		tipoAnimalAgregar = "";
		corralDestino1 = "";

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
		this.setTitle("Agregar Animales");
		this.setResizable(false);

		try {
			listadoCorrales = GestionCorralesINT.getNombreCorrales();
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}

		jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null,
				"Agregar Animales a un Corral",
				javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION,
				javax.swing.border.TitledBorder.DEFAULT_POSITION,
				new java.awt.Font("Tahoma", 0, 11))); // NOI18N

		jLabel1.setFont(new java.awt.Font("Tahoma", 0, 11));
		jLabel1.setText("Tipo de Animal");

		listadoTipoAnimal1.setFont(new java.awt.Font("Tahoma", 0, 11));
		listadoTipoAnimal1.setModel(new javax.swing.DefaultComboBoxModel(
				TipoAnimalEnum.getListadoTipos()));
		listadoTipoAnimal1.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				listadoTipoAnimal1ItemStateChanged(evt);
			}
		});

		jLabel2.setFont(new java.awt.Font("Tahoma", 0, 11));
		jLabel2.setText("Cantidad");

		cantidad1.setFont(new java.awt.Font("Tahoma", 0, 11));

		jLabel3.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jLabel3.setText("Corral de Destino");

		listadoCorrales1.setFont(new java.awt.Font("Tahoma", 0, 11));
		listadoCorrales1.setModel(new javax.swing.DefaultComboBoxModel(
				listadoCorrales));
		listadoCorrales1.addItemListener(new java.awt.event.ItemListener() {
			public void itemStateChanged(java.awt.event.ItemEvent evt) {
				listadoCorrales1ItemStateChanged(evt);
			}
		});

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout
				.setHorizontalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																false)
														.addComponent(
																listadoTipoAnimal1,
																javax.swing.GroupLayout.Alignment.LEADING,
																0,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																jLabel1,
																javax.swing.GroupLayout.Alignment.LEADING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(cantidad1)
														.addComponent(
																jLabel2,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addGap(18, 18, 18)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																jLabel3,
																javax.swing.GroupLayout.Alignment.TRAILING,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE)
														.addComponent(
																listadoCorrales1,
																javax.swing.GroupLayout.Alignment.TRAILING,
																0,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																Short.MAX_VALUE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));
		jPanel1Layout
				.setVerticalGroup(jPanel1Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel1Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(jLabel1)
														.addComponent(jLabel2)
														.addComponent(jLabel3))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED)
										.addGroup(
												jPanel1Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																listadoTipoAnimal1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																cantidad1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																21,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																listadoCorrales1,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																20,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)));

		jButton1.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		jButton1.setText("Cancelar");
		jButton1.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				jButton1ActionPerformed(evt);
			}
		});

		botonAgregar.setFont(new java.awt.Font("Tahoma", 0, 11)); // NOI18N
		botonAgregar.setText("Aceptar");
		botonAgregar.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {
				botonAgregarActionPerformed(evt);
			}
		});

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.TRAILING)
												.addGroup(
														layout.createSequentialGroup()
																.addComponent(
																		jButton1)
																.addGap(18, 18,
																		18)
																.addComponent(
																		botonAgregar))
												.addComponent(
														jPanel1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addContainerGap()
								.addComponent(jPanel1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.BASELINE)
												.addComponent(
														botonAgregar,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														21,
														javax.swing.GroupLayout.PREFERRED_SIZE)
												.addComponent(
														jButton1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														21,
														javax.swing.GroupLayout.PREFERRED_SIZE))
								.addContainerGap(
										javax.swing.GroupLayout.DEFAULT_SIZE,
										Short.MAX_VALUE)));

		pack();
	}// </editor-fold>

	private void listadoTipoAnimal1ItemStateChanged(java.awt.event.ItemEvent evt) {
		if (evt.getStateChange() == 1) {
			if (evt.getItem() == "Seleccione un tipo")
				tipoAnimalAgregar = "";
			else {
				tipoAnimalAgregar = evt.getItem().toString();
			}
		}
	}

	private void listadoCorrales1ItemStateChanged(java.awt.event.ItemEvent evt) {
		if (evt.getStateChange() == 1) {
			if (evt.getItem() == "Seleccione un Tipo")
				corralDestino1 = "";
			else {
				corralDestino1 = evt.getItem().toString();
			}
		}
	}

	private void botonAgregarActionPerformed(java.awt.event.ActionEvent evt) {
		try {
			if (StringUtils.isNullOrBlank(tipoAnimalAgregar))
				JOptionPane.showMessageDialog(this,
						ErrorUtils.SELECCIONE_ANIMAL, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			else if (StringUtils.isNullOrBlank(cantidad1.getText()))
				JOptionPane.showMessageDialog(this, ErrorUtils.CANTIDAD_VACIA,
						"ERROR", JOptionPane.ERROR_MESSAGE);
			else if (!StringUtils.isNumber(cantidad1.getText()))
				JOptionPane.showMessageDialog(this,
						ErrorUtils.CANTIDAD_INVALIDA, "ERROR",
						JOptionPane.ERROR_MESSAGE);
			else if (StringUtils.isNullOrBlank(corralDestino1))
				JOptionPane.showMessageDialog(this,
						ErrorUtils.SELECCIONE_NOMBRE_CORRAL, "ERROR",
						JOptionPane.ERROR_MESSAGE);

			else {
				int resultado;
				resultado = GestionAnimalesINT.cantidadAnimalesByTipoSinCorral(
						Integer.parseInt(cantidad1.getText()),
						tipoAnimalAgregar);

				if (resultado != -1) {
					JOptionPane.showMessageDialog(this,
							ErrorUtils.ANIMALES_SUELTOS_FALTANTES_1 + resultado
									+ " " + tipoAnimalAgregar + " "
									+ ErrorUtils.ANIMALES_SUELTOS_FALTANTES_2,
							"ERROR", JOptionPane.ERROR_MESSAGE);
				} else {

					Map<String, String> animales = new HashMap<String, String>();
					animales.put("tipoAnimal", tipoAnimalAgregar);
					animales.put("cantidad", cantidad1.getText());
					animales.put("corralDestino", corralDestino1);

					GestionCorralesINT.agregarAnimales(animales, this);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			JOptionPane.showMessageDialog(this, ErrorUtils.ERROR_INESPERADO,
					"ERROR INESPERADO", JOptionPane.ERROR_MESSAGE);
		}

	}

	private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
		if (JOptionPane.showConfirmDialog(this, ErrorUtils.CANCELAR,
				"CANCELAR", JOptionPane.YES_NO_OPTION) == 0)
			this.dispose();
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new AgregarAnimales().setVisible(true);
			}
		});
	}

	// Variables declaration - do not modify
	private javax.swing.JButton botonAgregar;
	private javax.swing.JTextField cantidad1;
	private javax.swing.JButton jButton1;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel2;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JComboBox listadoCorrales1;
	private javax.swing.JComboBox listadoTipoAnimal1;
	private String tipoAnimalAgregar;
	private String corralDestino1;
	private String[] listadoCorrales;
	// End of variables declaration
}
